<?php
		
	if(isset($_REQUEST['cartaction']))
	{
		$newCartItem = new UserCartItem();
		switch($_REQUEST['cartaction'])
		{
			case 'addtocart':
				$newCartItem->setItem($_REQUEST['product_code'],$currentUser->id);
				try
				{
					if($newCartItem->addItem())
					{
						$cartItemCount = $newCartItem->countCartItems();
						if($cartItemCount !== false)
						{
							$_SESSION['cartCount'] += 1;
							$existingProduct->code = $_REQUEST['product_code'];
							try
							{
								$existingProduct->retrieveProduct();
								$cartMessage = '<div><p><strong>'.$existingProduct->name.'</strong> added to cart.</p></div>';
							}
							catch(Exception $ex)
							{
								$cartMessage = '<div><p>An error occured Item was not added to cart.</p></div>';
							}
							//header("location: cart.php");
						}
					}
				}
				catch(Exception $ex)
				{
					throw new Exception($ex->getMessage());
				}
			break;
			
			case 'removefromcart':
				$newCartItem->setItem($_REQUEST['product_code'],$currentUser->id);
				try
				{
					if($newCartItem->removeItem())
					{
						$cartItemCount = $newCartItem->countCartItems();
						if($cartItemCount !== false)
						{
							$_SESSION['cartCount'] -= 1;
							$existingProduct->code = $_REQUEST['product_code'];
							try
							{
								$existingProduct->retrieveProduct();
								$cartMessage = '<div><p><strong>'.$existingProduct->name.'</strong> removed from cart.</p></div>';
							}
							catch(Exception $ex)
							{
								$cartMessage = '<div><p>Item removed from cart.</p></div>';
							}
						}
					}
				}
				catch(Exception $ex)
				{
					throw new Exception($ex->getMessage());
				}
			break;
			
			case 'viewcart':
				header("location: cart.php");
			break;
		}
	}
	if(isset($_POST['setQuantity']))
	{	
		$productValidationError = new validation();
		
		$quantityErrorMessage = '';
		$quantityErrorMessage = $productValidationError->isEmpty(trim($_POST['quantity']),'<label class="required">Enter a quantity.</label>');
		if(!$quantityErrorMessage)
		{
			$productValidationError->isEmpty(trim($_POST['quantity']),'<label class="required">Enter a quantity.</label>');
		}
		$userCart->setItem($_POST['product_code'],$currentUser->id,trim($_POST['quantity']));

		$formError = $productValidationError->status;
		
		if($formError==0)
		{
			try
			{
				$itemAsProduct = $userCart->getItem();
				if($itemAsProduct)
				{
					if(($itemAsProduct["on_hand"] >= $_POST["quantity"]) && $_POST["quantity"]>0)
					{
						if($userCart->updateItem())
						{
							$cartMessage = '<div><p><strong>'.$itemAsProduct["name"].'</strong> quantity is updated to '.$_POST["quantity"].'.</p></div>';
						}
						else
						{
							$cartMessage  = '<div><p>An error occured. '.$itemAsProduct["name"].' purchase quantity was not updated.</p></div>';
						}
					}
					else if($itemAsProduct["on_hand"] ==$_POST["quantity"])
					{
						$cartMessage = '<div><p><strong>'.$itemAsProduct["name"].'</strong> quantity is updated to '.$_POST["quantity"].'.</p></div>';
					}
					else if($_POST["quantity"] <= 0)
					{
						$cartMessage = '<div><p>Invalid purchase quantity choosen for <strong>'.$itemAsProduct["name"].'</strong>.</p></div>';
					}
					else
					{
						$cartMessage = '<div><p class="">There is only '.$itemAsProduct["on_hand"].' '.$itemAsProduct["name"].' in stock. Choose a lower quantity.</p></div>';
					}
				}
			}
			catch(Exception $ex)
			{
				$cartMessage  = $ex->getMessage();
			}
		}
	}
?>